package eu.vranckaert.workRegistrationWidget.activities;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;
import com.google.inject.Inject;
import eu.vranckaert.workRegistrationWidget.R;
import eu.vranckaert.workRegistrationWidget.service.RegistrationService;
import eu.vranckaert.workRegistrationWidget.util.DateUtil;
import eu.vranckaert.workRegistrationWidget.util.Preferences;
import roboguice.activity.GuiceActivity;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author Dirk Vranckaert
 *         Date: 7-jun-2010
 *         Time: 11:31:42
 */
public class ExportActivity extends GuiceActivity {
    private static final String LOG_TAG = ExportActivity.class.getName();

    private static final String DEFAULT_FILE_NAME = "timeRegistrations-";

    private static final int DIALOG_SELECT_START_DATE = 0;
    private static final int DIALOG_SELECT_END_DATE = 1;
    private static final int DIALOG_STARTDATE_BEFORE_ENDDATE = 2;
    private static final int DIALOG_ENDDATE_BEFORE_STARTDATE = 3;
    private static final int DIALOG_ERROR_SAVING_FILE = 4;

    private DatePickerDialog.OnDateSetListener startDateCallback;
    private DatePickerDialog.OnDateSetListener endDateCallback;

    private Calendar startCal;
    private Calendar endCal;

    @Inject
    RegistrationService registrationService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.export);
        init();
    }

    private void init() {
        setStartDate();
        setEndDate();
        setDefaultFileName();
        setExportAction();
    }

    private void setExportAction() {
        Button finishButton = (Button) findViewById(R.id.regExportFinish);
        finishButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                export();
            }
        });
    }

    private void export() {
        EditText fileName = (EditText) findViewById(R.id.regExportFileName);

        File file = registrationService.export(startCal, endCal, fileName.getText().toString(), this);

        if (file != null) {
            Toast.makeText(this, getString(R.string.regExportSuccess, file.getAbsolutePath()), Toast.LENGTH_LONG).show();
            finish();
        } else {
            showDialog(DIALOG_ERROR_SAVING_FILE);
        }
    }

    private void setStartDate() {
        startCal = new GregorianCalendar();
        startCal.setTime(new Date());
        startCal.set(Calendar.DAY_OF_MONTH, 1);

        final Button startButton = (Button) findViewById(R.id.exportStartDateTxt);

        startDateCallback = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                Calendar tmpStartCal = new GregorianCalendar();
                tmpStartCal.set(Calendar.YEAR, year);
                tmpStartCal.set(Calendar.MONTH, month);
                tmpStartCal.set(Calendar.DAY_OF_MONTH, day);

                if (tmpStartCal.getTime().compareTo(endCal.getTime()) <= 0) {
                    Log.d(LOG_TAG, "Updating the start date");
                    startCal.set(Calendar.YEAR, year);
                    startCal.set(Calendar.MONTH, month);
                    startCal.set(Calendar.DAY_OF_MONTH, day);
                    setDateOnButton(startButton, startCal.getTime());
                } else {
                    showDialog(DIALOG_ENDDATE_BEFORE_STARTDATE);
                }
            }
        };

        setDateOnButton(startButton, startCal.getTime());
        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(DIALOG_SELECT_START_DATE);
            }
        });
    }

    private void setEndDate() {
        endCal = new GregorianCalendar();
        endCal.setTime(new Date());

        final Button endButton = (Button) findViewById(R.id.exportEndDateTxt);

        endDateCallback = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                Calendar tmpEndCal = new GregorianCalendar();
                tmpEndCal.set(Calendar.YEAR, year);
                tmpEndCal.set(Calendar.MONTH, month);
                tmpEndCal.set(Calendar.DAY_OF_MONTH, day);

                if (startCal.getTime().compareTo(tmpEndCal.getTime()) <= 0) {
                    Log.d(LOG_TAG, "Updating the end date");
                    endCal.set(Calendar.YEAR, year);
                    endCal.set(Calendar.MONTH, month);
                    endCal.set(Calendar.DAY_OF_MONTH, day);
                    setDateOnButton(endButton, endCal.getTime());
                } else {
                    showDialog(DIALOG_STARTDATE_BEFORE_ENDDATE);
                }
            }
        };

        setDateOnButton(endButton, endCal.getTime());
        endButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(DIALOG_SELECT_END_DATE);
            }
        });
    }

    private void setDateOnButton(Button button, Date date) {
        button.setText(DateUtil.formatDateFull(date, this));
    }

    private void setDefaultFileName() {
        EditText fileName = (EditText) findViewById(R.id.regExportFileName);
        fileName.setText(Preferences.getExportFileName(this));
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        Dialog dialog = null;

        switch(id) {
            case DIALOG_SELECT_START_DATE:
                dialog = new DatePickerDialog(this, startDateCallback, startCal.get(Calendar.YEAR), startCal.get(Calendar.MONTH), startCal.get(Calendar.DAY_OF_MONTH));
                break;
            case DIALOG_SELECT_END_DATE:
                dialog = new DatePickerDialog(this, endDateCallback, endCal.get(Calendar.YEAR), endCal.get(Calendar.MONTH), endCal.get(Calendar.DAY_OF_MONTH));
                break;
            case DIALOG_STARTDATE_BEFORE_ENDDATE:
                AlertDialog.Builder alertBuilder1 = new AlertDialog.Builder(this);
                alertBuilder1.setTitle(R.string.warning)
                           .setMessage(R.string.regExport_exc_enddate_before_startdate)
                           .setCancelable(false)
                           .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.cancel();
                                }
                            });
                AlertDialog alertDialog1 = alertBuilder1.create();
                dialog = alertDialog1;
                break;
            case DIALOG_ENDDATE_BEFORE_STARTDATE:
                AlertDialog.Builder alertBuilder2 = new AlertDialog.Builder(this);
                alertBuilder2.setTitle(R.string.warning)
                           .setMessage(R.string.regExport_exc_enddate_before_startdate)
                           .setCancelable(false)
                           .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.cancel();
                                }
                            });
                AlertDialog alertDialog2 = alertBuilder2.create();
                dialog = alertDialog2;
                break;
            case DIALOG_ERROR_SAVING_FILE:
                AlertDialog.Builder alertBuilder3 = new AlertDialog.Builder(this);
                alertBuilder3.setTitle(R.string.warning)
                           .setMessage(R.string.regExport_exc_could_not_write)
                           .setCancelable(false)
                           .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.cancel();
                                }
                            });
                AlertDialog alertDialog3 = alertBuilder3.create();
                dialog = alertDialog3;
                break;
        }

        return dialog;
    }
}
